package com.googlecode.scenemaker3d.view
{
	import org.papervision3d.core.geom.Lines3D;
	import org.papervision3d.core.geom.renderables.Line3D;
	import org.papervision3d.core.geom.renderables.Vertex3D;
	import org.papervision3d.materials.special.LineMaterial;
	import org.papervision3d.objects.DisplayObject3D;
	
	public class Grid3D extends DisplayObject3D
	{
		private var lm:LineMaterial;
		private var lines:Lines3D;
		//Creates grid in the XZ plane
		public function Grid3D(width:Number, divs:Number)
		{
			
			lm = new LineMaterial(0x909090, .2);
			lines = new Lines3D(lm);
			
			draw(width, divs);
		}
		public function draw(width:Number, divs:Number):void{
			var dist:Number = width / divs;
			var offSet:Number = width / 2;
			var lines:Lines3D = new Lines3D(lm);
			
			for(var i:int = 0; i<divs+1; i++){
				var v1:Vertex3D = new Vertex3D(i*dist - offSet, 0, -offSet);
				var v2:Vertex3D = new Vertex3D(i*dist - offSet, 0, offSet);
				var l:Line3D = new Line3D(lines, lm, 1, v1, v2);
				
				lines.addLine(l);
				
				
				//trace(l+i+"up");
			}
			
			for(i = 0; i<divs+1; i++){
				v1 = new Vertex3D(-offSet, 0, i*dist - offSet);
				v2 = new Vertex3D(offSet, 0, i*dist - offSet);
				l  = new Line3D(lines, lm, 1, v1, v2);
				lines.addLine(l);
				//trace(l+i+"across");
			}
			this.addChild(lines);		
		}
	}
}